package net.openl10n.mdf;

import java.util.LinkedHashSet;
import java.util.Set;

import net.openl10n.mdf.immutable.ImmutableBuilder;
import net.openl10n.mdf.immutable.ImmutableStateException;

public class ReferencePartBuilder extends AbstractDocumentPartBuilder implements ImmutableBuilder<ReferencePart>{
	
	private ReferenceId reference;
	
	public ReferencePartBuilder() {
	}
	
	public ReferencePartBuilder(ReferencePart referencePart) {
		super(referencePart);
		reference = referencePart.getReference();
	}
	
	public boolean isValid() {
		if(! super.isValid()) return false;
		return reference != null;
	}
	
	public void validate() throws ImmutableStateException {
		if(! isValid()) {
			Set<String> invalidFields = new LinkedHashSet<String>();
			super.populateInvalidFields(invalidFields);
			if(reference == null)
				invalidFields.add("reference");
			throw new ImmutableStateException(invalidFields);
		}
	}
	
	@Override
	public ReferencePart build() throws ImmutableStateException{
		validate();
		return new ReferencePart(id, version, reference);
	}

}
